package java_class_work.week08.internet.upimage;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author Tiam
 * @Date 2022/11/1 15:12
 * @Description:
 */
public class TcpServer {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();

            InputStream in = socket.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(new File(".\\src\\java_class_work\\iotest\\up.jpg"),true);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len=in.read(bytes))!=-1){
                // 将上传的文件写入
                fileOutputStream.write(bytes,0,len);
            }
            out.println("图片上传成功!");
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
